package com.tim.app.ui.adapter.message;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.tim.app.R;
import com.tim.app.server.entry.Service;
import com.tim.app.util.BitmapLoader;

import java.util.List;


/**
 * 官方帐号
 */
public class ServiceAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Service> {

    private Context mContext;

    public ServiceAdapter(Context mContext, List<Service> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_service, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, int position, Service data) {
        if (data == null) {
            return;
        }
        final ViewHolder holder = (ViewHolder) mHolder;

        if (!TextUtils.isEmpty(data.getName())) {
            holder.tvUserName.setText(data.getName());
        }
        if (!TextUtils.isEmpty(data.getDesc())) {
            holder.tvLastMessageDesc.setText(data.getDesc());
        }
        if (data.getId() == 1) {
            holder.tvFlag.setVisibility(View.VISIBLE);
            holder.rivAvatar.setImageResource(R.drawable.ic_official);
            if (data.getUnReadCount() > 0) {
                holder.tvUnReadMessageCount.setVisibility(View.VISIBLE);
                holder.tvUnReadMessageCount.setText(String.valueOf(data.getUnReadCount()));
            } else {
                holder.tvUnReadMessageCount.setVisibility(View.GONE);
            }
        } else {
            holder.tvFlag.setVisibility(View.GONE);
            holder.rivAvatar.setImageResource(R.drawable.ic_service);
        }
    }

    public class ViewHolder extends BaseRecyclerViewHolder {

        RoundedImageView rivAvatar;
        TextView tvLastMessageTime, tvUnReadMessageCount;
        TextView tvUserName, tvLastMessageDesc;
        RelativeLayout llRight;
        TextView tvFlag;

        public ViewHolder(View itemView) {
            super(itemView);
            rivAvatar = (RoundedImageView) itemView.findViewById(R.id.rivAvatar);
            tvLastMessageTime = (TextView) itemView.findViewById(R.id.tvLastMessageTime);
            tvUnReadMessageCount = (TextView) itemView.findViewById(R.id.tvUnReadMessageCount);
            llRight = (RelativeLayout) itemView.findViewById(R.id.llRight);
            tvUserName = (TextView) itemView.findViewById(R.id.tvUserName);
            tvFlag = (TextView) itemView.findViewById(R.id.tvFlag);
            tvLastMessageDesc = (TextView) itemView.findViewById(R.id.tvLastMessageDesc);
        }

    }

}
